from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponse
from bson import json_util
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required

@login_required
@permission_required(perm='suifang.view_suifang')  #判断用户有无查看随访人员名单的权限，如果没有就不能查看，对应left_menu.html中的perms.XXX
def suifang(request): #基线脑出血问卷
    import pymongo
    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient["question"]
    mycol = mydb["questionnaire1"]
    questionnaire = []
    question = []
    for x in mycol.find({}, {'_id': 0, '姓名': 1, '性别': 1, '出生日期': 1, '身份证号码': 1}):  # 查询特定字段
        # for x in mycol.find():
        question.append(list(x))
        questionnaire.append(x)

    excelTitle = []
    for x in mycol.find({}, {'_id': 0}):  # 查询特定字段
        excelTitle.append(list(x))
        if len(excelTitle) > 2:
            break

    return render(request,'suifang/suifang_list.html',
                  {"questionnaire": json_util.dumps(questionnaire), "question": json_util.dumps(question[0]),
                   "excelTitle": json_util.dumps(excelTitle[0])})


#脑出血资源库
@login_required
def resource_database(request):
    return render(request, "resource_database.html")